package com.weirong.smartvault.backservice.service;

import com.weirong.smartvault.dao.ForeignCurrencyTypeRepository;
import com.weirong.smartvault.entity.ForeignCurrencyType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ForeignCurrencyTypeService {

    @Autowired
    private ForeignCurrencyTypeRepository foreignCurrencyTypeRepository;

    public Iterable<ForeignCurrencyType> getAll()
    {
        return this.foreignCurrencyTypeRepository.findAll();
    }

    public Optional<String> getForeignCurrencyTypeNameByCode(String code)
    {
        List<ForeignCurrencyType> foreignCurrencyTypes = this.foreignCurrencyTypeRepository.findForeignCurrencyTypeByCode(code);
        if (foreignCurrencyTypes == null || foreignCurrencyTypes.isEmpty()) return Optional.empty();
        return  Optional.of(foreignCurrencyTypes.get(0).getName());

    }

    public Optional<ForeignCurrencyType> getForeignCurrencyTypeByCode(String code)
    {
        List<ForeignCurrencyType> foreignCurrencyTypes = this.foreignCurrencyTypeRepository.findForeignCurrencyTypeByCode(code);
        if (foreignCurrencyTypes == null || foreignCurrencyTypes.isEmpty()) return Optional.empty();
        return Optional.of(foreignCurrencyTypes.get(0));
    }

    public int getForeignCurrencyTypeCount()
    {
        return (int) this.foreignCurrencyTypeRepository.count();
    }

}